package server;


import model.Questions;
import model.User;

import java.io.*;
import java.util.*;

/**
 * 编程实现数据的存取
 */
public class ServerDao {

    /**
     * 编程实现管理员账号和密码的校验并将结果返回出去
     *
     * @param user
     * @return
     */
    public boolean serverManagerCheck(User user) {
        if ("admin".equals(user.getUserName()) && "123456".equals(user.getPassword())) {
            return true;
        }
        return false;
    }

    /**
     * 编程实现学账号和密码的校验并将结果返回出去
     *
     * @param user
     * @return
     */
    public boolean serverStudentCheck(User user) throws IOException, ClassNotFoundException {
        List<User> users = readUsers();
        for (User u : users) {
            if (u.getUserName().equals(user.getUserName()) && u.getPassword().equals(user.getPassword()))
                return true;
        }

        return false;
    }

    /**
     * 修改密码并判断是否成功
     *
     * @param user
     * @return
     */
    public boolean updatePassword(User user) throws IOException, ClassNotFoundException {
        List<User> users = readUsers();
        for (User u : users) {
            if (u.getUserName().equals(user.getUserName())){
                users.remove(u);
                users.add(user);
                serverSaveStudents(users);
                return true;
            }
        }
        return false;//先定义成功
    }

    /**
     * 自定义成员方法读取考题---->"./questions"
     *
     * @return
     */
    public synchronized Map<String, String> readQuestions() throws IOException, ClassNotFoundException {
        Map<String, String> questions = new TreeMap<>();
/*        //测试题目 c'c'c'c'c
        questions.put("第1题答案A","A");
        questions.put("第2题答案A","A");
        questions.put("第3题答案B","B");
        questions.put("第4题答案C","C");
        questions.put("第5题答案D","D");*/
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("./questions.txt"));
        questions = (Map<String, String>) ois.readObject();
        return questions;
    }

    /**
     * 读取服务器保存的用户名文件。保存在./users.txt
     *
     * @return
     */
    public synchronized List<User> readUsers() throws IOException, ClassNotFoundException {
        List<User> users = new ArrayList<>();
/*        //测试程序
        users.add(new User("name1","123456"));
        users.add(new User("name2","123456"));
        users.add(new User("name1","123456"));
        users.add(new User("name1","123456"));
        *///读取文件内容
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("./users.txt"));
        //读取学员信息
        users = (List<User>) ois.readObject();

        return users;
    }

    /**
     * 增加学员
     *
     * @param user
     */
    public synchronized boolean serverAddStudent(User user) throws IOException, ClassNotFoundException {
        List<User> users = readUsers();//读取数据
        for (User t : users) {
            //如果用户名存在，则添加失败
            if (t.getUserName().equals(user.getUserName())) {
                return false;
            }
        }
        //如果用户名不存在，则保存到服务器用户名文件，并返回true;
        users.add(user);
        serverSaveStudents(users);
        return true;
    }

    /**
     * 保存学员信息
     *
     * @param users
     */
    private synchronized void serverSaveStudents(List<User> users) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("./users.txt"));
        oos.writeObject(users);
        System.out.println(new Date() + "：保存学员信息成功");
    }

    /**
     * 删除学员
     *
     * @param name
     * @return
     */
    public synchronized boolean deleteByName(String name) throws IOException, ClassNotFoundException {
        List<User> users = readUsers();//读取数据
        for (User u : users) {
            if (u.getUserName().equals(name)) {//如果要删除的学员存在
                users.remove(u);
                serverSaveStudents(users);
                return true;
            }
        }
        return false;
    }

    /**
     * 管理员更新学员密码
     *
     * @param user
     * @return
     */
    public synchronized boolean updateByManager(User user) throws IOException, ClassNotFoundException {
        List<User> users = readUsers();//读取数据
        for (User u : users) {
            if (u.getUserName().equals(user.getUserName())) {//如果要删除的学员存在
                int index = users.indexOf(u);
                users.set(index, user);
                serverSaveStudents(users);
                return true;
            }
        }
        return false;
    }

    /**
     * 按名字查找
     *
     * @param s
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public synchronized List<User> serverSearchByName(String s) throws IOException, ClassNotFoundException {
        List<User> users = readUsers();//读取数据
        List<User> usersByName = new ArrayList<>();
        for (User u : users) {
            if (u.getUserName().contains(s)) {
                usersByName.add(u);
            }
        }
        return usersByName;
    }

    /**
     * 增加考题
     *
     * @param question
     * @param answer
     * @return
     * @throws IOException
     */
    public synchronized boolean addQuestion(String question, String answer) throws IOException, ClassNotFoundException {
        Map<String, String> questions = readQuestions();
        questions.put(question, answer);
        saveQuestions(questions);//保存
        return true;
    }

    /**
     * 保存考题
     *
     * @param questions
     * @throws IOException
     */
    private synchronized void saveQuestions(Map<String, String> questions) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("./questions.txt"));
        oos.writeObject(questions);
    }

    /**
     * 删除考题
     *
     * @param question
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public synchronized boolean deletQuestion(String question) throws IOException, ClassNotFoundException {
        Map<String, String> questions = readQuestions();
        questions.remove(question);//删除
        saveQuestions(questions);//保存
        return true;
    }

    /**
     * 更新考题
     *
     * @param question
     * @param newquestion
     * @param answer
     * @throws IOException
     */
    public synchronized void updateQuestion(String question, String newquestion, String answer) throws IOException, ClassNotFoundException {
        Map<String, String> questions = readQuestions();
        questions.remove(question);
        questions.put(newquestion, answer);
        saveQuestions(questions);//保存
    }

    /**
     * 查找考题
     *
     * @param search
     * @return
     */
    public synchronized Map<String, String> serverSearchQuestions(String search) throws IOException, ClassNotFoundException {
        Map<String, String> questions = readQuestions();
        Map<String, String> results = new TreeMap<>();
        for (var v : questions.keySet()) {
            if (v.contains(search)) {
                results.put(v, questions.get(v));
            }
        }
        return results;
    }

    /**
     * 客户端批量导入题目
     *
     * @param inputQuestions
     */
    public void inputQues(Map<String, String> inputQuestions) throws IOException, ClassNotFoundException {
        Map<String, String> questions = readQuestions();
        questions.putAll(inputQuestions);
        saveQuestions(questions);//保存
    }
}
